/**
 *  Copyright (C) 2011
 *  This file is part of Bioinformatics Sequence Comparison Game.
 *
 *  Bioinformatics Sequence Comparison Game is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Bioinformatics Sequence Comparison Game is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Bioinformatics Sequence Comparison Game.  If not, see <http://www.gnu.org/licenses/>.
 *  
 *  @author Eric Augustine
 *  @author Jim Jensen
 *  @author Spencer Vanderpoel
 *  @author Corban McCammon
 *  @author Brett Penza
 *  @version 0.1.0
 */
package com.project.BSCG1;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

/*
 * Main menu activity that kicks off when the application is started
 */
public class BioInformaticSCGActivity extends Activity {
    
	
	private Button singleplayer; //Contains button that initiates a single player game
	private Button multiplayer; //Contains button that initiates multiplayer game
	private Button faqButton; // button opens the FAQ html page
	
	/** Sets the layout of the activity and initializes the button controls */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initControls();
    }
    @Override
    protected void onStart(){
    	super.onStart();
    }
    @Override
    protected void onResume() {
        super.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
    }
    @Override
    protected void onStop() {
        super.onStop();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
    
    /*
     * Sets up the controls when singleplayer button is clicked. The singleplayer brings up an alert
     * dialog prompting for user name which is passed into the GameActivity Intent that is called
     * after the user accepts the box
     */
    private void initControls(){
    	//Find the start button
    	singleplayer = (Button)findViewById(R.id.startGame);
    	multiplayer = (Button)findViewById(R.id.multiPlayer);
    	faqButton = (Button)findViewById(R.id.about);
    	
    	//Set the listener for the button
    	singleplayer.setOnClickListener(new Button.OnClickListener() { 
    		//Prompt for user name and start Game Activity
    		public void onClick (View v){ 
    			
    			//Create alert
    			AlertDialog.Builder userName = new AlertDialog.Builder(v.getContext());

    			//Set title and message
    			userName.setTitle("User Name");
    			userName.setMessage("Enter Your name");
    			
    			//Set up text field
    			final EditText inputField = new EditText(v.getContext());
    			userName.setView(inputField);
    			
    			final View temp = v;
    			final Bundle userInfo = new Bundle();
    			
    			//Set up a confirm button in alert
    			userName.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    				//Packages the user Name into a bundle and passes it into the gameActivity
    				public void onClick(DialogInterface dialog, int whichButton) {
    					
    					String userName = inputField.getText().toString().replaceAll("\n", "");
    					userInfo.putString("userName", userName);
    					tutorialPrompt(temp,userInfo);
      				}
    			});
    			
    			//Set up cancel button that exits alert and does nothing
    			userName.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    			  public void onClick(DialogInterface dialog, int whichButton) {
    			  }
    			});
    			
    			//Show alert
    			userName.show();
    			
    			
    		}
    	});
    	
    	multiplayer.setOnClickListener(new Button.OnClickListener() { 
    		//Prompt for user name and start Game Activity
    		public void onClick (View v){ 
    			
    			//Create alert
    			AlertDialog.Builder numberOfPlayers = new AlertDialog.Builder(v.getContext());

    			//Set title and message
    			numberOfPlayers.setTitle("Number of Players");
    			numberOfPlayers.setMessage("How many players (2-4)?");
    			
    			//Set up text field
    			final EditText inputField = new EditText(v.getContext());
    			numberOfPlayers.setView(inputField);
    			
    			final View temp = v;
    			
    			//Set up a confirm button in alert
    			numberOfPlayers.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    				//Packages the user Name into a bundle and passes it into the gameActivity
    				public void onClick(DialogInterface dialog, int whichButton) {
    					
    					String numOfPlayers = inputField.getText().toString().replaceAll("\n", "");
    					if(numOfPlayers.matches("2|3|4")){
    						int num = Integer.parseInt(numOfPlayers);
    						promptsPlayersForNames(temp, num);
    					}else{
    						//Create alert
    		    			AlertDialog.Builder invalid = new AlertDialog.Builder(temp.getContext());

    		    			//Set title and message
    		    			invalid.setTitle("Error");
    		    			invalid.setMessage("Invalid number of players.");
    		    			
    		    			invalid.setPositiveButton("Enter", new DialogInterface.OnClickListener() {
			    				//Packages the user Name into a bundle and passes it into the gameActivity
			    				public void onClick(DialogInterface dialog, int whichButton) {
			      				}
    		    			});
    		    			
    		    			invalid.show();
    					}
      				}
    			});
    			
    			//Set up cancel button that exits alert and does nothingtem
    			numberOfPlayers.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    			  public void onClick(DialogInterface dialog, int whichButton) {
    			  }
    			});
    			
    			//Show alert
    			numberOfPlayers.show();
    			
    			
    		}
    	});
    	
    	faqButton.setOnClickListener(new Button.OnClickListener() {
    		public void onClick(View v) {
    			Intent intent = new Intent(getApplication(),WebViewActivity.class);
    			startActivity(intent);
    		}
    	});
    }
    
    public void promptsPlayersForNames(final View v, int num){
    	ArrayList<EditText> names = new ArrayList<EditText>();
    	//Create alert
		AlertDialog.Builder userName = new AlertDialog.Builder(v.getContext());

		//Set title and message
		userName.setTitle("Multiplayer");
		userName.setMessage("Enter Player's Names:");
		
		LinearLayout linearLayout = new LinearLayout(this);
		linearLayout.setLayoutParams( new  LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
		                LinearLayout.LayoutParams.FILL_PARENT));
		linearLayout.setOrientation(1);
		
		for(int i = 0; i < num; i++){
			names.add(new EditText(v.getContext()));
			TextView t = new TextView(v.getContext());
			t.setText("Player " + (i+1));
			linearLayout.addView(t);
			linearLayout.addView(names.get(i));
		}
		userName.setView(linearLayout);
		
		final ArrayList<EditText> name = names;
		final Bundle info = new Bundle();
		
		//Set up a confirm button in alert
		userName.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			//Packages the user Name into a bundle and passes it into the gameActivity
			public void onClick(DialogInterface dialog, int whichButton) {
				ArrayList<String> userNames = new ArrayList<String>();
				for(int i = 0; i < name.size(); i++){
					userNames.add(name.get(i).getText().toString().replaceAll("\n", ""));
				}
				info.putStringArrayList("playerNames", userNames);
				tutorialPromptM(v,info);
				
			}
		});
		
		//Set up cancel button that exits alert and does nothing
		userName.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
		  public void onClick(DialogInterface dialog, int whichButton) {
		  }
		});
		
		//Show alert
		userName.show();
    	
    }
    
    public void tutorialPrompt(View v, final Bundle userInfo){
    	//Create alert
		AlertDialog.Builder tutorialMode = new AlertDialog.Builder(v.getContext());

		//Set title and message
		tutorialMode.setTitle("Tutorial Mode Enabled");
		tutorialMode.setMessage("Enable Tutorial Mode?");
		
		//Set up a confirm button in alert
		tutorialMode.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			//Packages the user Name into a bundle and passes it into the gameActivity
			public void onClick(DialogInterface dialog, int whichButton) {
				
				Intent intent = new Intent(getApplication(),GameActivity.class);
				userInfo.putBoolean("TutorialMode", true);
				intent.putExtras(userInfo);
				startActivity(intent); 
				}
		});
		
		//Set up cancel button that exits alert and does nothing
		tutorialMode.setNegativeButton("No", new DialogInterface.OnClickListener() {
		  public void onClick(DialogInterface dialog, int whichButton) {
				Intent intent = new Intent(getApplication(),GameActivity.class);
				userInfo.putBoolean("TutorialMode", false);
				intent.putExtras(userInfo);
				startActivity(intent); 
		  }
		});
		
		tutorialMode.show();
    }
    
    /*
     * For multiplayer, delete when multiplayer is finished
     */
    public void tutorialPromptM(View v, final Bundle userInfo){
    	//Create alert
		AlertDialog.Builder tutorialMode = new AlertDialog.Builder(v.getContext());

		//Set title and message
		tutorialMode.setTitle("Tutorial Mode Enabled");
		tutorialMode.setMessage("Enable Tutorial Mode?");
		
		//Set up a confirm button in alert
		tutorialMode.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			//Packages the user Name into a bundle and passes it into the gameActivity
			public void onClick(DialogInterface dialog, int whichButton) {
				
				Intent intent = new Intent(getApplication(), MultiplayerActivity.class);
				userInfo.putBoolean("TutorialMode", true);
				intent.putExtras(userInfo);
				startActivity(intent); 
				}
		});
		
		//Set up cancel button that exits alert and does nothing
		tutorialMode.setNegativeButton("No", new DialogInterface.OnClickListener() {
		  public void onClick(DialogInterface dialog, int whichButton) {
				Intent intent = new Intent(getApplication(), MultiplayerActivity.class);
				userInfo.putBoolean("TutorialMode", false);
				intent.putExtras(userInfo);
				startActivity(intent); 
		  }
		});
		
		tutorialMode.show();
    }
}